package com.justerdu.mvpclient.login.presenter;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.justerdu.mvpclient.login.model.IUserModel;
import com.justerdu.mvpclient.login.model.UserModel;
import com.justerdu.mvpclient.login.view.IUserView;
/**
 * @Author JusterDu 2016/11/19.
 * @Func User Presenter层接口实现
 */
public class UserPresenter implements IUserPresenter {
	IUserView iUserView;
	IUserModel userModel;
	Handler handler;
	private static final String TAG = "UserPresenter";

	public UserPresenter(IUserView iLoginView) {
		this.iUserView = iLoginView;
		userModel = new UserModel();
		handler = new Handler(Looper.getMainLooper());
	}

	@Override
	public void clear() {
		iUserView.onClearText();
	}

	@Override
	public void doLogin(String username, String password) {
		Log.d(TAG, "doLogin: " + "username: " +  username + "  password: " + password);
		String loginStatus = userModel.login(username,password);
		Log.d(TAG, "doLogin: loginStatus: " + loginStatus);
		if(loginStatus.equals("NETWORK_ERROR")){
			loginStatus = "NETWORK_ERROR";
		}else if(loginStatus.equals("success")){
			loginStatus = "SUCCESS";
		}else{
			loginStatus = "ERROR";
		}
		final String result = loginStatus;
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				iUserView.onLoginResult(result);
			}
		}, 1000);


	}



	@Override
	public void setProgressBarVisiblity(int visiblity){
		iUserView.onSetProgressBarVisibility(visiblity);
	}

}
